<?php 
defined('IN_IXDCW') or exit('Access Denied');
class Job extends BasicModule{

	/** @var 模块id */
	private $_moduleid;

	/** @var 条目ID */
	private $_itemid;

	/** @var string 表格 */
	private $_table;

	/** @var string 表格数据 */
	private $_tableData;

	/** @var 是否内容分表 */
	private $_split;

	/** @var array 可用字段 */
	private $_fields;


	function __construct($moduleid) {
		parent::__construct();
		global $table, $table_data, $MOD;
		$this->_moduleid  = $moduleid;
		$this->_table     = $table;
		$this->_tableData = $table_data;
		$this->_split     = $MOD['split'];
		$this->_fields = array('catid','areaid','level','title','style','fee','introduce','department','total','minsalary','maxsalary','type','gender','marriage','education','minage','maxage','experience','status','hits','username','truename','telephone', 'mobile','address','email','msn','qq',
			'ali','skype','sex','totime','editor','addtime','adddate','edittime','editdate','ip','template','linkurl','filepath','note','company');
    }
	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	function pass($post) {
		global $DT_TIME, $MOD;
		if(!is_array($post)) return false;
		if(!$post['catid']) return $this->err(lang('message->pass_cate'));
		if(strlen($post['title']) < 3) return $this->err(lang('message->pass_job_title'));
		if(!$post['areaid']) return $this->err(lang('message->pass_job_areaid'));
		if(!$post['content']) return $this->err(lang('message->pass_job_content'));
		if($post['totime']) {
			if(!is_date($post['totime'])) return $this->err(lang('message->pass_date'));
			$totime = strtotime($post['totime'].' 23:59:59');
			if($totime < $DT_TIME) return $this->err(lang('message->pass_todate'));
		}
		if(!$post['truename']) return $this->err(lang('message->pass_truename'));
		//if(!$post['telephone']) return $this->err(lang('message->pass_telephone'));
		//if(!is_email(trim($post['email']))) return $this->err(lang('message->pass_email'));
		return true;
	}

	function set($post) {
		global $MOD, $DT_TIME, $DT_IP, $TYPE, $_username, $r;
		$post['editor'] = $_username;
		$post['addtime'] = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['adddate'] = timetodate($post['addtime'], 3);
		$post['edittime'] = $DT_TIME;
		$post['editdate'] = timetodate($post['edittime'], 3);
		$post['totime'] = $post['totime'] ? strtotime($post['totime'].' 23:59:59') : 0;
		$post['fee'] = dround($post['fee']);
		$post['total'] = intval($post['total']);
		$post['email'] = trim($post['email']);
		$post['minsalary'] = intval($post['minsalary']);
		$post['maxsalary'] = intval($post['maxsalary']);
		$post['type'] = intval($post['type']);
		$post['gender'] = intval($post['gender']);
		$post['education'] = intval($post['education']);
		$post['experience'] = intval($post['experience']);
		$post['minage'] = intval($post['minage']);
		$post['maxage'] = intval($post['maxage']);
		$post['title'] = trim($post['title']);
		$post['content'] = stripslashes($post['content']);
		if($MOD['clear_link']) $post['content'] = Post::clearLink($post['content']);
		if($MOD['save_remotepic']) $post['content'] = Post::saveRemote($post['content']);
		if($MOD['introduce_length']) $post['introduce'] = addslashes(Post::intro($post['content'], $MOD['introduce_length']));
		if($this->_itemid) {
			$r = $this->getOne();
		} else {
			$post['ip'] = $DT_IP;
		}
		if(!defined('DT_ADMIN')) {
			$content = $post['content'];
			unset($post['content']);
			$post = Str::htmlSpecialChars($post);
			$post['content'] = Str::safe($content);
		}
		$post['content'] = addslashes($post['content']);
		return $post;
	}

	function getOne() {
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
        return $this->_db->getOne("SELECT * FROM {$this->_table} a,{$content_table} c WHERE a.itemid=c.itemid and a.itemid=$this->_itemid");
	}

	function getList($condition = 'status=3', $order = 'edittime DESC', $cache = '') {
		global $MOD,$CATEGORY,$items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$r['alt'] = $r['title'];
			$r['title'] = set_style($r['title'], $r['style']);
			$r['userurl'] = userurl($r['username']);
			$r['parentid'] = $CATEGORY[$r['catid']]['parentid'] ? $CATEGORY[$r['catid']]['parentid'] : $r['catid'];
			$lists[] = $r;
		}
		return $lists;
	}

	function add($post) {
		global $MOD;
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$this->_itemid = $this->_db->insertId();
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
		$this->_db->query("INSERT INTO {$content_table} (itemid,content) VALUES ('$this->_itemid', '$post[content]')");
		$this->update($this->_itemid, $post, $post['content']);
		if($post['status'] == 3 && $post['username'] && $MOD['credit_add']) {
			credit_add($post['username'], $MOD['credit_add']);
			credit_record($post['username'], $MOD['credit_add'], 'system', lang('my->credit_record_add', array($MOD['name'])), 'ID:'.$this->_itemid);
		}
		return $this->_itemid;
	}

	function edit($post) {
		global $r, $_userid;
		$this->delete($this->_itemid, false);
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table, $post, "itemid={$this->_itemid}", '', $this->_fields));
		$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
	    $this->_db->query(Sql::update($content_table,"content='$post[content]'","itemid=$this->_itemid"));
		$this->update($this->_itemid, $post, $post['content']);
		EditorUpload::clearDiff($post['content'], $r['content'], $_userid);
		return true;
	}


	function update($itemid, $item = array(), $content = '') {
		$item or $item = $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid=$itemid");
		$keyword = $item['title'].','.$item['department'].','.strip_tags(cat_pos($item['catid'], ',')).strip_tags(area_pos($item['areaid'], ','));
		$keyword = str_replace("//", '', addslashes($keyword));
		$item['itemid'] = $itemid;
		$sql = "keyword='$keyword'";
		if($item['username']) {
			$m = userinfo($item['username']);
			if($m) $sql .= ",groupid='$m[groupid]',company='$m[company]',vip='$m[vip]',validated='$m[validated]'";
		}
		$this->_db->query(Sql::update($this->_table,"$sql","itemid=$itemid"));
	}

	function recycle($itemid) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->recycle($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=0","itemid=$itemid"));
			$this->delete($itemid, false);
			return true;
		}		
	}

	function restore($itemid) {
		global $module, $MOD;
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->restore($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=3","itemid=$itemid"));
			return true;
		}		
	}

	function delete($itemid, $all = true) {
		global $MOD;
		if(is_array($itemid)) {
			foreach($itemid as $v) {
				$this->delete($v, $all);
			}
		} else {
			$this->_itemid = $itemid;
			$r = $this->getOne();
			if($all) {
				$userid = get_user($r['username']);
				if ($r['content']) EditorUpload::clearDiff('', $r['content'], $userid);
				$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
				$content_table = content_table($this->_moduleid, $this->_itemid, $this->_split, $this->_tableData);
				$this->_db->query(Sql::delete($content_table,"itemid=$itemid"));
				if($r['username'] && $MOD['credit_del']) {
					credit_add($r['username'], -$MOD['credit_del']);
					credit_record($r['username'], -$MOD['credit_del'], 'system', lang('my->credit_record_del', array($MOD['name'])), 'ID:'.$this->_itemid);
				}
			}
		}
	}

	function check($itemid) {
		global $_username, $DT_TIME, $MOD;;
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->check($v); }
		} else {
			$this->_itemid = $itemid;
			$item = $this->getOne();
			if($MOD['credit_add'] && $item['username'] && $item['hits'] < 1) {
				credit_add($item['username'], $MOD['credit_add']);
				credit_record($item['username'], $MOD['credit_add'], 'system', lang('my->credit_record_add', array($MOD['name'])), 'ID:'.$this->_itemid);
			}
			$editdate = timetodate($DT_TIME, 3);
			$this->_db->query(Sql::update($this->_table,"status=3,hits=hits+1,editor='$_username',edittime=$DT_TIME,editdate='$editdate'","itemid=$itemid"));
			return true;
		}
	}

	function reject($itemid) {
		global $_username, $DT_TIME;
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->reject($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=1,editor='$_username'","itemid=$itemid"));
			return true;
		}
	}

	function expire($condition = '') {
		global $DT_TIME;
		$this->_db->query(Sql::update($this->_table,"status=4","status=3 AND totime>0 AND totime<$DT_TIME $condition"));
	}

	function clear($condition = 'status=0') {		
		$result = $this->_db->query("SELECT itemid FROM {$this->_table} WHERE $condition ");
		while($r = $this->_db->fetchArray($result)) {
			$this->delete($r['itemid']);
		}
	}

	function level($itemid, $level) {
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$this->_db->query(Sql::update($this->_table,"level=$level","itemid IN ($itemids)"));
	}

	function refresh($itemid) {
		global $DT_TIME;
		$editdate = timetodate($DT_TIME, 3);
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$this->_db->query(Sql::update($this->_table,"edittime='$DT_TIME',editdate='$editdate'","itemid IN ($itemids)"));
	}

	function _update($username) {
		global $DT_TIME;
		$this->_db->query(Sql::update($this->_table,"status=4","status=3 AND totime>0 AND totime<$DT_TIME AND username='$username'"));
	}

	/**
	 * 所有分站更新数据（将旧数据编辑时间更新到现在）
	 * @param $num 每个分站修改条数
	 * auther Sameal
	 */
	function randomUpdateTime($num) {
		global $DT_TIME,$AREA;
		$editdate = timetodate($DT_TIME, 3);
		foreach($this->_db->getAll("SELECT * FROM {$this->_db->pre}city") as $city){
			$areaids = $AREA[$city['areaid']]['arrchildid'];
			$this->_db->query(Sql::update($this->_table,"addtime='$DT_TIME',edittime='$DT_TIME',editdate='$editdate'","areaid IN ($areaids) ORDER BY RAND() LIMIT $num"));
		}
	}
}
?>